package LeetCode.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: leiwenkang
 * @date: 2020/6/27
 * @time: 13:26
 */
public class Test205同构字符串 {
    public static void main(String[] args) {
        System.out.println(isIsomorphic("ab", "aa"));
    }

    public static boolean isIsomorphic(String s, String t) {
        return isomorphicValue(s).equals(isomorphicValue(t));
    }


    private static String isomorphicValue(String str) {
        StringBuilder sb = new StringBuilder();
        Map<Character, Integer> map = new HashMap<>();
        int index = 0;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (map.containsKey(ch)) {
                sb.append(map.get(ch));
            } else {
                sb.append(++index);
                map.put(ch, index);
            }
        }

        return sb.toString();
    }
}
